using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Data;
 using System.Data.SqlClient;
 using MinhTam.DataAccess.Web;
//───────────────────────────────────────────────────────────────────────────────────────
 namespace MinhTam.Objects
  { 
 public class USER_GROUP:  SQLConnectWeb { 
 public static string sTableName= "USER_GROUP"; 
   public string USER_GROUP_ID;
   public string GROUP_NAME;
   public string IS_ADMIN;
   #region DataColumn Name ;
 public static  string cl_USER_GROUP_ID="USER_GROUP_ID" ;
 public static  string cl_GROUP_NAME="GROUP_NAME" ;
 public static  string cl_IS_ADMIN="IS_ADMIN" ;
 #endregion;
//───────────────────────────────────────────────────────────────────────────────────────
       public USER_GROUP() {}
//───────────────────────────────────────────────────────────────────────────────────────
       public USER_GROUP(
         string sUSER_GROUP_ID,
         string sGROUP_NAME,
         string sIS_ADMIN){
         this.USER_GROUP_ID= sUSER_GROUP_ID;
         this.GROUP_NAME= sGROUP_NAME;
         this.IS_ADMIN= sIS_ADMIN;
}
//───────────────────────────────────────────────────────────────────────────────────────
       public static USER_GROUP Create_USER_GROUP ( string sUSER_GROUP_ID  ){
    DataTable dt=SearchByUSER_GROUP_ID(sUSER_GROUP_ID) ;
    if(dt!=null && dt.Rows.Count>0) 
      return new USER_GROUP(dt,0);
      return null;
}
//───────────────────────────────────────────────────────────────────────────────────────
   private static string s_Select()
    {
   return " SELECT T.* FROM USER_GROUP AS T";
    }
//───────────────────────────────────────────────────────────────────────────────────────
 public USER_GROUP( DataTable table,int pos)
{
         this.USER_GROUP_ID= table.Rows[pos]["USER_GROUP_ID"].ToString();
         this.GROUP_NAME= table.Rows[pos]["GROUP_NAME"].ToString();
         this.IS_ADMIN= table.Rows[pos]["IS_ADMIN"].ToString();
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_GROUP_ID(string sUSER_GROUP_ID)
{
          string sqlSelect= s_Select()+ " WHERE USER_GROUP_ID  ="+ sUSER_GROUP_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_GROUP_ID(string sUSER_GROUP_ID,string sMatch)
{
          string sqlSelect= s_Select()+ " WHERE USER_GROUP_ID"+ sMatch +sUSER_GROUP_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByGROUP_NAME(string sGROUP_NAME)
{
          string sqlSelect= s_Select()+ " WHERE GROUP_NAME  Like N'%"+ sGROUP_NAME + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByIS_ADMIN(string sIS_ADMIN)
{
          string sqlSelect= s_Select()+ " WHERE IS_ADMIN  Like N'%"+ sIS_ADMIN + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable Search( string sUSER_GROUP_ID
            , string sGROUP_NAME
            , string sIS_ADMIN
            )
 {
       string sqlselect=s_Select() + " WHERE" ;
      if (sUSER_GROUP_ID!=null && sUSER_GROUP_ID!="") 
            sqlselect +=" AND USER_GROUP_ID =" + sUSER_GROUP_ID ;
      if (sGROUP_NAME!=null && sGROUP_NAME!="") 
            sqlselect +=" AND GROUP_NAME LIKE N'%" + sGROUP_NAME +"%'" ;
      if (sIS_ADMIN!=null && sIS_ADMIN!="") 
            sqlselect +=" AND IS_ADMIN LIKE N'%" + sIS_ADMIN +"%'" ;
   sqlselect=sqlselect.Replace("WHERE AND","WHERE");
   int n=sqlselect.IndexOf("WHERE");
   if(n==sqlselect.Length -5) sqlselect=sqlselect.Remove(n,5) ;
   return GetTable(sqlselect,sTableName);
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static USER_GROUP Insert_Object(
string  sGROUP_NAME
            ,string  sIS_ADMIN
            ) 
 { 
              string tem_sGROUP_NAME=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sGROUP_NAME,"nvarchar");
              string tem_sIS_ADMIN=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sIS_ADMIN,"char");

             string sqlSave=" INSERT INTO USER_GROUP("+
                   "GROUP_NAME," 
        +                   "IS_ADMIN) VALUES("
 +tem_sGROUP_NAME+","
 +tem_sIS_ADMIN +")";
             bool OK = Exec(sqlSave)>=1?true:false;
           if (OK) 
           { 
          USER_GROUP newUSER_GROUP= new USER_GROUP();
                 newUSER_GROUP.USER_GROUP_ID=GetTable( " SELECT TOP 1 USER_GROUP_ID FROM USER_GROUP ORDER BY USER_GROUP_ID DESC ").Rows[0][0].ToString();
              newUSER_GROUP.GROUP_NAME=sGROUP_NAME;
              newUSER_GROUP.IS_ADMIN=sIS_ADMIN;
            return newUSER_GROUP; 
           } 
           else return null ;
}
//───────────────────────────────────────────────────────────────────────────────────────
public bool  Save_Object(string sGROUP_NAME
                ,string sIS_ADMIN
                ) 
 { 
              string tem_sGROUP_NAME=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sGROUP_NAME,"nvarchar");
              string tem_sIS_ADMIN=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sIS_ADMIN,"char");

 string sqlSave=" UPDATE USER_GROUP SET "+"GROUP_NAME ="+tem_sGROUP_NAME+","
 +"IS_ADMIN ="+tem_sIS_ADMIN+" WHERE USER_GROUP_ID="+MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(this.USER_GROUP_ID,"int identity");;
              bool OK = Exec(sqlSave)>=1?true:false;
           if (OK) 
           { 
                this.GROUP_NAME=sGROUP_NAME;
                this.IS_ADMIN=sIS_ADMIN;
           } 
 return OK;  }
//───────────────────────────────────────────────────────────────────────────────────────
 #region Update DataColumn  
 public bool Update_USER_GROUP_ID(string sUSER_GROUP_ID)
{
    string sqlSave= " UPDATE USER_GROUP SET USER_GROUP_ID='"+ sUSER_GROUP_ID+ "' WHERE USER_GROUP_ID='"+ this.USER_GROUP_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.USER_GROUP_ID=sUSER_GROUP_ID;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_GROUP_NAME(string sGROUP_NAME)
{
    string sqlSave= " UPDATE USER_GROUP SET GROUP_NAME='N"+ sGROUP_NAME+ "' WHERE USER_GROUP_ID='"+ this.USER_GROUP_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.GROUP_NAME=sGROUP_NAME;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_IS_ADMIN(string sIS_ADMIN)
{
    string sqlSave= " UPDATE USER_GROUP SET IS_ADMIN='N"+ sIS_ADMIN+ "' WHERE USER_GROUP_ID='"+ this.USER_GROUP_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.IS_ADMIN=sIS_ADMIN;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 #endregion
 #region Update DataColumn  Static 
 public static bool Update_GROUP_NAME(string sGROUP_NAME,string s_USER_GROUP_ID)
{
  string sqlSave= " UPDATE USER_GROUP SET GROUP_NAME='N"+sGROUP_NAME+"' WHERE USER_GROUP_ID='"+ s_USER_GROUP_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_IS_ADMIN(string sIS_ADMIN,string s_USER_GROUP_ID)
{
  string sqlSave= " UPDATE USER_GROUP SET IS_ADMIN='N"+sIS_ADMIN+"' WHERE USER_GROUP_ID='"+ s_USER_GROUP_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
#endregion
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable GetTableAll() 
 {
       return  GetTableAll(null, null);
 }
public static DataTable GetTableAll(string sWhere, params string[] orderFields)
{
   string sqlSelect = " SELECT * FROM USER_GROUP";
   if (!string.IsNullOrEmpty(sWhere))
      sqlSelect += " where " + sWhere; 
   string order = "";
   if (orderFields != null && orderFields.Length > 0)
     order = string.Join(",", orderFields);
   if (order != "")
      sqlSelect += " ORDER BY " + order;
   return GetTable(sqlSelect,sTableName);
}
//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
public static DataTable GetTableFields(string sWhere, string[] orderFields, params string[] fields)
{
 string field = "";
 if (fields != null && fields.Length > 0)
    field = string.Join(",", fields);
 else field = "*";
 string sqlSelect = string.Format(" SELECT {0} FROM {1} ", field, "USER_GROUP");
 if (!string.IsNullOrEmpty(sWhere))
    sqlSelect += " where " + sWhere;
 string order = "";
 if (orderFields != null && orderFields.Length > 0)
    order = string.Join(",", orderFields);
 if (order != "")
    sqlSelect += " ORDER BY " + order;
 return GetTable(sqlSelect,sTableName);
 }
 public static DataTable GetTableFields(params string[] fields)
 {
    return GetTableFields(null, null, fields);
 }
 public static DataTable GetTableFields(string[] orderFields, params string[] fields)
 {
    return GetTableFields(null, orderFields, fields);
 }
//───────────────────────────────────────────────────────────────────────────────────────
   private static DataTable dt_USER_GROUP;
   public static bool Change_dt_USER_GROUP = true;
   public static bool AllowAutoChange = true;
   public static DataTable get_USER_GROUP()
   {
   if (dt_USER_GROUP == null || Change_dt_USER_GROUP == true)
     {
   dt_USER_GROUP = GetTableAll();
         Change_dt_USER_GROUP = true && AllowAutoChange ;
     }
     return dt_USER_GROUP;
   }
   //───────────────────────────────────────────────────────────────────────────────────────
}  
 } 
